﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;


namespace LaneLpnr.Common
{
    public class WriteLog
    {

        ConfigHelper config = new ConfigHelper();
        public const int Max_Log = 1024 * 1024 * 20;

        public void WriteLogFile(string input)
        {
            // string ipaddress = LPNRTool.GetManchineIPAddress;

            string LogPath = ConfigHelper.GetConnectionStringConfig("logpath");//读取配置文件内容
            string filename = ConfigHelper.GetConnectionStringConfig("logfilename");
            string filepathname = System.IO.Path.Combine(LogPath, filename);

            if (!Directory.Exists(LogPath) && LogPath != null && LogPath != "")
            {
                System.IO.Directory.CreateDirectory(LogPath);
            }

            FileInfo finfo = new FileInfo(filepathname);
            if (finfo.Exists && finfo.Length > Max_Log)
            {
                string NewFileName = LogPath + "\\" + filename + "bak";
                FileInfo NewFile = new FileInfo(NewFileName);
                if (NewFile.Exists)
                {
                    NewFile.Delete();
                }
                File.Move(filepathname, NewFileName);
            }
            System.IO.FileStream fs = new System.IO.FileStream(filepathname, System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite);
            //using (System.IO.FileStream fs = new System.IO.FileStream(filepathname, System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite))

            {
                //fs.Flush();
                string logLine = System.String.Format("{0:G}: {1}", System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"), input + "\r\n");

                //byte[] msg = new byte[fs.Length];
                //byte[] msg = new byte[1024];

                byte[] msg;
                if (logLine.Length != 0)
                {
                    msg = new byte[logLine.Length];
                }
                else
                {
                    msg = new byte[1024];
                }
                msg = Encoding.UTF8.GetBytes(logLine);
                fs.Write(msg, 0, msg.Length);
                fs.Close();
            }
        }

        public static void WriteLogstatic(string input)
        {
            try
            {
                int Max_Log = 1024 * 1024 * 10;
                string ipaddress = Forms.车道视频监控.GetManchineIPAddress;
                string LogPath = ConfigHelper.GetConnectionStringConfig("logpath");//读取配置文件内容
                string filename = ipaddress + " " + ConfigHelper.GetConnectionStringConfig("logfilename");
                string filepathname = System.IO.Path.Combine(LogPath, filename);


                if (!Directory.Exists(LogPath))
                {
                    System.IO.Directory.CreateDirectory(LogPath);
                }

                FileInfo finfo = new FileInfo(filepathname);
                if (finfo.Exists && finfo.Length > Max_Log)
                {
                    string NewFileName = LogPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "Logbak.log";
                    FileInfo NewFile = new FileInfo(NewFileName);
                    if (NewFile.Exists)
                    {
                        NewFile.Delete();
                    }
                    File.Move(filepathname, NewFileName);
                }

                System.IO.FileStream fs = new System.IO.FileStream(filepathname, System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite);

                //fs.Flush();
                string logLine = System.String.Format("{0:G}: {1}", System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"), input + "\r\n");

                //byte[] msg = new byte[fs.Length];
                byte[] msg = new byte[1024];
                msg = Encoding.UTF8.GetBytes(logLine);
                fs.Write(msg, 0, msg.Length);
                fs.Close();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message.ToString());
            }
        }


        public static void WriteLogstatic(string input, string name)
        {
            try
            {
                string LogPath = "";
                switch (name)
                {
                    case "Com":
                        LogPath = Common.ConfigHelper.GetConnectionStringConfig("Comlogpath");//读取配置文件内容
                        break;
                    case "Lpnr":
                        LogPath = Common.ConfigHelper.GetConnectionStringConfig("LPNRlogpath");//读取配置文件内容
                        break;
                    case "Alb":
                        LogPath = Common.ConfigHelper.GetConnectionStringConfig("Alblogpath");//读取配置文件内容
                        break;
                    case "Vehicle":
                        LogPath = Common.ConfigHelper.GetConnectionStringConfig("Vehiclelogpath");//读取配置文件内容
                        break;
                    case "Mysql":
                        LogPath = Common.ConfigHelper.GetConnectionStringConfig("Mysqllogpath");//读取配置文件内容
                        break;
                    case "Led":
                        LogPath = Common.ConfigHelper.GetConnectionStringConfig("Ledlogpath");//读取配置文件内容
                        break;
                    case "Mob":
                        LogPath = Common.ConfigHelper.GetConnectionStringConfig("Moblogpath");
                        break;
                    case "Pay":
                        LogPath = Common.ConfigHelper.GetConnectionStringConfig("Paylogpath");
                        break;
                    case "ExcErro":
                        LogPath = Common.ConfigHelper.GetConnectionStringConfig("ExcErro");
                        break;
                    default:
                        LogPath = System.Windows.Forms.Application.StartupPath;
                        break;
                }


                string filename = Common.ConfigHelper.GetConnectionStringConfig("logfilename");
                string filepathname = System.IO.Path.Combine(LogPath, filename);

                if (!Directory.Exists(LogPath))
                {
                    System.IO.Directory.CreateDirectory(LogPath);
                }

                FileInfo finfo = new FileInfo(filepathname);

                if (finfo.Exists && finfo.Length > Max_Log)
                {
                    //string NewFileName = LogPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "Logbak.log";
                    string NewFileName = LogPath + "\\" + filename + "bak";
                    FileInfo NewFile = new FileInfo(NewFileName);
                    if (NewFile.Exists)
                    {
                        NewFile.Delete();
                    }
                    File.Move(filepathname, NewFileName);
                }

                System.IO.FileStream fs = new System.IO.FileStream(filepathname, System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite);

                string logLine = "";
                //fs.Flush();
                if (name == "else")
                {
                    logLine = System.String.Format("{0:G}, {1}", System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"), input + "\r\n");

                }
                else
                {
                    logLine = System.String.Format("{0:G}: {1}", System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"), input + "\r\n");
                }


                //byte[] msg = new byte[fs.Length];
                //byte[] msg = new byte[1024];
                byte[] msg;
                if (logLine.Length != 0)
                {
                    msg = new byte[logLine.Length];
                }
                else
                {
                    msg = new byte[1024];
                }
                msg = Encoding.UTF8.GetBytes(logLine);
                fs.Write(msg, 0, msg.Length);
                fs.Close();

                msg = null;
            }
            catch (Exception ex)
            {
                FlashLog.FlashLogger.Debug("异常 WriteLogstatic" + ex.ToString() + "\r\n");

            }
            finally
            {
                GC.Collect();
            }
        }


        /// <summary>
        /// 写入抓拍机ip地址 至caramList
        /// </summary>
        /// <param name="str"></param>
        public void writeIpList(string str)
        {
            string pathname = writeorreadIpList();

            using (FileStream wfs = new FileStream(pathname, System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite))
            {
                wfs.Flush();
                string content = str + "\n";
                byte[] msg = new byte[str.Length];
                msg = Encoding.UTF8.GetBytes(content);
                wfs.Write(msg, 0, msg.Length);
                wfs.Close();
            }
        }

        public string returnipList = "";
        public string readIpList()
        {
            string str = "";
            string pathname = writeorreadIpList();
            using (FileStream fs = new FileStream(pathname, FileMode.OpenOrCreate, FileAccess.Read))
            {
                StreamReader sr = new StreamReader(fs);
                while ((str = sr.ReadLine()) != null)
                {
                    returnipList += "\n" + str;
                }
                sr.Close();
                fs.Close();
            }
            return returnipList;
        }


        public string writeorreadIpList()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string name = "camera_ip.lst";
            string pathname = Path.Combine(path, name);

            return pathname;
        }



    }
}
